<?php
include dirname(dirname(dirname(__FILE__))).'/vendor/jpush/autoload.php';
use JPush\Client as JPush;

class Helpers
{
    /**
     * 生成无线递归树下拉框或数据
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <2016-7-28 20:48>
     * @param   <obj>               <$pidListRes>   [数据库结果集无线分类]
     * @param   <string>            <$strOrArr>     [根据参数判断返回select字符串还是数组]
     * @param   <array>             <$keyLis>       [下表模型数据库中对应的id,pid,path,title]
     * @des     <根据调用方传递的数据库无线分类生成select下拉框字符串或者返回生成的数组>
     * @return  <str or arr>        <$return>       [返回的字符串或者数组]
     */
    public static function createTreeSelect($pidListRes,$strOrArr='str',$keyLis = array('id'=>'id','pid'=>'pid','path'=>'path','title'=>'title'))
    {
        if(empty($pidListRes)) return $strOrArr == 'str' ? '' : [];
        $str = '';
        $arr = [];
        foreach($pidListRes as $k => $v)
        {
            $pid = $keyLis['pid'];
            $id = $keyLis['id'];
            $path = $keyLis['path'];
            $title = $keyLis['title'];
            if($v->$pid != 0)
            {
                $forCout = count(explode(',',$v->$path));
                $preStr = '';
                for($i=0 ;$i < $forCout ;$i++)
                {
                    $preStr.= '----';
                }
                $v->$title = $preStr.'|&nbsp;&nbsp;'.$v->$title;
                $str .= '<option value="'.$v->$id.'_'.$v->$path.'">'.$v->$title.'</option>';
                $arr[$k] = $v;
            }else{
                $str .= '<option value="'.$v->$id.'_'.$v->$path.'">'.$v->$title.'</option>';
                $arr[$k] = $v;
            }
        }
        $return = $strOrArr == 'str' ? $str : $arr;
        return $return;
    }


    /**
     * 公共DB操作
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @des     <描述:后台控制器调用查询方法节省代码量>
     * @param   <string>    <$tableName>
     * @param   <string>    <$select>
     * @param   <string>    <$raw>
     * @param   <array>     <$where>
     * @param   <string>    <$orderBy>
     * @param   <int>       <$first>
     * @return  <obj>       <$res>
     */
    public static function DbForTree($tableName,$select = '*',$raw = '',$where=array(),$orderBy='',$first=0)
    {
        $res = DB::table($tableName);
        $res = !empty($raw) ? $res->Select(DB::raw($raw)) : $res->select($select);
        if(!empty($where))
        {
            foreach($where as $k => $v)
            {
                $res->where($k,$v['sb'],$v['v']);
            }
        }
        if(!empty($orderBy)) $res->orderBy($orderBy);
        $res = !empty($first) ? $res->first() : $res->get();
        return $res;
    }

    /**
     * 功能说明...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <2016-7-28 22:45>
     * @des     <描述:...>
     * @param $view
     * @param $flag
     * @param $pageOrButton
     * @param $data
     * @return mixed
     */
    public static function showViewCURD($view,$flag,$pageOrButton,$data)
    {
        $data['flag'] = $flag;
        if(!empty($pageOrButton)) $data['pageOrButton'] = $pageOrButton;
        return view($view)->withData($data);
    }

    /**
     * 上传文件功能...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:>
     * @param $inputFileName    <接受上传文件的input表单名称>
     * @param $passUpFileType   <允许的文件上传类型attay('image/jpg'...)>
     * @param $uploadPath       <上传文件的目录>
     * @param $newFileNamw      <上传文件的新文件名>
     * @return int
     */
    public static function uploadFile($inputFileName,$passUpFileType,$uploadPath,$newFileNamw)
    {
        $file = \Input::file($inputFileName);
        //验证图片上传
        if(!empty($file) && $file -> isValid()){
            //获取文件后缀
            $entension = $file -> getClientOriginalExtension();
            //获取文件上传类型
            $mimeTye = $file->getClientMimeType();
            if($file->getSize() > 1024*1024*2)
            {
                //超过限定尺寸2M
                return -2;
            }else if(!in_array($mimeTye,$passUpFileType) || !in_array('image/'.$entension,$passUpFileType)){
                //图片上传类型有误
                return -3;
            }else{
                //检测是否存在目录
                if(self::mkDirs($uploadPath))
                {
                    $newFileNamw = $newFileNamw.'.'.$entension;
                    $path = $file -> move($uploadPath,$newFileNamw);
                    if($path)
                    {
                        return $path->getPathname();
                    }
                    //上传失败
                    return -5;
                }else{
                    //文件目录不存在
                    return -4;
                }
            }
        }else{
            return -1;
        }
    }

    /**
     * 递归新建文件夹
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:递归新建文件夹>
     * @param $dir  <文件目录>
     * @return bool
     */
    public static function mkDirs($dir){
        if(!is_dir($dir)){
            if(!self::mkDirs(dirname($dir))){
                return false;
            }
            if(!mkdir($dir,0777)){
                return false;
            }
        }
        return true;
    }

    /**
     * 无线递归获取导航栏目数组...
     * @author  <ZhengYaFeng>
     * @data    <2016-10-11 15:01>
     * @access  <public>
     * @des     <描述:无线递归获取导航栏目数组>
     * @param int $pid
     * @return array
     */
    public static function formatMenu($pid = 0)
    {
        $res = DB::table('menulist')
                    ->where('status',1)
                    ->where('showwhere',1)
                    ->where('pid',$pid)
                    ->get();
        $arr = array();
        if(!empty($res))
        {
            foreach ($res as $v)
            {
                $v->child = self::formatMenu($v->id);
                $arr[] = $v;
            }
        }
        return $arr;
    }

    /**
     * 无线地柜导航下拉生成...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:>
     * @param $arr
     * @return string
     */
    public static function createMenu($arr)
    {
        $str = '';
        if(!empty($arr))
        {
            foreach ($arr as $v)
            {
                $str .= '<li class="menu-item menu-item-type-taxonomy menu-item-object-category">';
                $url = empty($v->url) ? $v->route : $v->url;
                $title = empty($v->title) ? '' : $v->title;
                $str .= '<a href="'.$url.'">'.$title.'</a>';
                if(!empty($v->child))
                {
                    $str .= '<ul>';
                    $str .= self::createMenu($v->child);
                    $str .= '</ul>';
                }
                $str .= '</li>';
            }
        }
        return $str;
    }

    public static function AdminMenuLeft()
    {
        return [
            '/admini'                   =>  '',
            '/admini/menumgmt'          =>  '<h1>常用操作<small>网站栏目管理</small></h1>',
            '/admini/article'           =>  '<h1>常用操作<small>文章管理</small></h1>',
            '/admini/bannermanage'      =>  '<h1>常用操作<small>广告轮播管理</small></h1>',
            '/admini/tag'               =>  '<h1>常用操作<small>标签管理</small></h1>',
            '/admini/adminuser'         =>  '<h1>RBAC权限管理<small>管理员列表</small></h1>',
            '/admini/adminusergroup'    =>  '<h1>RBAC权限管理<small>用户组管理</small></h1>',
            '/admini/adminusernode'     =>  '<h1>RBAC权限管理<small>权限节点管理</small></h1>',
            '/admini/datadd'            =>  '<h1>系统设置<small>数据字典</small></h1>',
            '/admini/systemlog'         =>  '<h1>系统设置<small>系统日志管理</small></h1>',
            '/admini/fileManage'        =>  '<h1>附件管理</h1>',
            '/admini/piwik'             =>  '<h1>Piwik管理</h1>'

        ];
    }

    /**
     * 无线下拉辅助函数...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:无线下拉辅助函数>
     * @param array $list
     * @param string $select_id
     * @param int $is_select
     * @param int $no_top
     * @return string
     */
    public static function getTreeSelect($list = array(), $select_id = '', $is_select = 0, $no_top = 0)
    {
        $str = '';
        if ($no_top == 0) {
            if ($is_select == 1 && 0 == $select_id) {
                $str = '<option value="0_0" selected="selected">顶级</option>';
            } else {
                $str = '<option value="0_0">顶级</option>';
            }
        }
        //组装数据样式
        foreach ($list as $key => $value) {
            if ($is_select == 1 && $value->id == $select_id) {
                $str .= '<option value="' . $value->path . '_' . $value->id . '" selected="selected">';
            } else {
                $str .= '<option value="' . $value->path . '_' . $value->id . '">';
            }
            if (!empty($value->path)) {
                $num = count(explode(',', $value->path));

                for ($i = 0; $i < $num; $i++) {
                    $str .= '------';
                }
                $str .= '├&nbsp;&nbsp;' . $value->name;

            } else {
                $str .= $value->name;
            }
            $str .= '</option>';
        }

        return $str;
    }

    /**
     * 生成左侧菜单栏...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $data
     * @return string
     */
    public static function leftMenu($data)
    {
        $data = self::object_to_array($data);
        $data = self::list_to_tree($data);
        return self::showList($data);
    }

    /**
     * 生成菜单字符串...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $data
     * @return string
     */
    public static function showList($data)
    {
        static $str = '';
        if (empty($data))
            return $str;
        foreach ($data as $k => $v) {
            if (!empty($v['_child'])) {
                $str .= '<li class="treeview"><a href="' . $v['node_name'] . '"><i class="fa fa-book"></i><span>' . $v['name'] . '</span><i class="fa fa-angle-left pull-right"></i></a>';
                $str .= '<ul class="treeview-menu">';
                self::showList($v['_child']);
                $str .= '</ul>';
            } else {
                $str .= '<li class="treeview"><a href="' . $v['node_name'] . '"><i class="fa fa-book"></i><span>' . $v['name'] . '</span></a>';
            }
            $str .= '</li>';
        }

        return $str;
    }

    /**
     * 对象专数组...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $obj
     * @return array
     */
    public static function object_to_array($obj)
    {
        $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
        $arr = array();
        if (!empty($_arr)) {
            foreach ($_arr as $key => $val) {
                $val = (is_array($val)) || is_object($val) ? self::object_to_array($val) : $val;
                $arr[$key] = $val;
            }
        }

        return $arr;
    }
    /**
     * 把返回的数据集转换成Tree
     * @param array $list 要转换的数据集
     * @param string $pid parent标记字段
     * @param string $level level标记字段
     * @return array
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    private  static function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = & $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = & $refer[$parentId];
                        $parent[$child][] = & $list[$key];
                    }
                }
            }
            $tree = self::sortData($tree,$child);
        }
        return $tree;
    }

    /**
     * 排序...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $tree
     * @param string $child
     * @return mixed
     */
    public static function sortData($tree,$child = '_child')
    {
        foreach ($tree as $key => $val){
            if (isset($val[$child])){
                self::sortData($val[$child]);
                usort($val[$child], "menu_sort");
                $tree[$key][$child] = $val[$child];
            }
        }
        return $tree;
    }

    /**
     * 公共日志写入方法
     * @author <ZhengYaFeng> <544780158@qq.com>
     * @param <string> <$modules> <操作模块标识>
     * @param <string> <$action> <操作行为>
     * @param <string> <$id> <操作id>
     * @param <string> <$status> <success or error>
     * @param <array> <$param> <数组操作时接受到的参数>
     * @return <null>
     */
    public static function insertUserActionLog($modules = '', $action = '', $id = '', $status = 'success', $pram = array(),$isgetcity=0)
    {
        date_default_timezone_set('Asia/ShangHai');
        $arr = array(
            'adminLogin' => '<font color="blue">【后台管理系统】</font>',
            'rbacUser' => '<font color="blue">【RBAC权限管理】</font> 《《 <font color="blue">[管理员列表模块]</font>',
            'rbacGroup' => '<font color="blue">【RBAC权限管理】</font> 《《 <font color="blue">[用户组管理模块]</font>',
            'rbacNode' => '<font color="blue">【RBAC权限管理】</font> 《《 <font color="blue">[权限节点管理模块]</font>',
            'dataDD' => '<font color="blue">【系统设置】</font> 《《 <font color="blue">[数据字典模块]</font>',
            'menuList' => '<font color="blue">【常用操作】</font> 《《 <font color="blue">[网站栏目管理模块]</font>',
            'article' => '<font color="blue">【常用操作】</font> 《《 <font color="blue">[文章管理模块]</font>',
            'msgManage' => '<font color="blue">【常用操作】</font> 《《 <font color="blue">[留言管理模块]</font>',
            'fileUpload' => '<font color="blue">【附件管理】</font> 《《 <font color="blue">[上传新文件模块]</font>',
            'zipManage' => '<font color="blue">【附件管理】</font> 《《 <font color="blue">[附件管理模块]</font>',
            'fileManage' => '<font color="blue">【附件管理】</font> 《《 <font color="blue">[文件式管理器模块]</font>',
            'bannerManage' => '<font color="blue">【常用操作】</font> 《《 <font color="blue">[广告轮播管理模块]</font>',
            'msgManage' =>  '<font color="blue">【常用操作】</font> 《《 <font color="blue">[留言管理]</font>',
            'systemInfo' => '<font color="blue">【系统设置】</font> 《《 <font color="blue">[系统参数设置]</font>',
            'tag' => '<font color="blue">【常用操作】</font> 《《 <font color="blue">[标签管理模块]</font>',
            'proportions' =>'<font color="blue">【其他配置】</font> 《《 <font color="blue">[分成比例配置]</font>',
            'personalcomm' => '<font color="blue">【其他配置】</font> 《《 <font color="blue">[E聊天主播绿色直播承诺书设置]</font>',
            'userargee_con' => '<font color="blue">【其他配置】</font> 《《 <font color="blue">[用户协议设置]</font>',
            'banner' => '<font color="blue">【其他配置】</font> 《《 <font color="blue">[banner管理]</font>',
            'member' => '<font color="blue">【用户管理】</font> 《《 <font color="blue">[用户管理]</font>',
            'member_normal' => '<font color="blue">【用户管理】</font> 《《 <font color="blue">[认证审核]</font>',
            'member_reject' => '<font color="blue">【用户管理】</font> 《《 <font color="blue">[认证拒绝]</font>',
            'member_auth_sign' => '<font color="blue">【用户管理】</font> 《《 <font color="blue">[主播认证标签]</font>',
            'weekmanage' => '<font color="blue">【周周榜管理】</font> 《《 <font color="blue">[周周榜管理]</font>',
            'level' => '<font color="blue">【等级管理】</font> 《《 <font color="blue">[等级管理]</font>',
            'levelforanchor' => '<font color="blue">【等级管理】</font> 《《 <font color="blue">[主播等级管理]</font>',
            'videoMonitor' => '<font color="blue">【视频聊天监控】</font> 《《 <font color="blue">[直播视频监控]</font>',
            'recharge_rate' => '<font color="blue">【充值管理】</font> 《《 <font color="blue">[充值档次]</font>',
            'member_withdraw' => '<font color="blue">【提现管理】</font> 《《 <font color="blue">[提现管理]</font>',
            'ImgMonitor' => '<font color="blue">【视频聊天监控】</font> 《《 <font color="blue">[直播截图监控]</font>',
            'present' => '<font color="blue">【消费管理】</font> 《《 <font color="blue">[礼物管理]</font>',
            'report' => '<font color="blue">【举报管理】</font> 《《 <font color="blue">[举报管理]</font>',
            'noinfophone_con' => '<font color="blue">【其他配置】</font> 《《 <font color="blue">[消息来电无提醒]</font>',
            'callproblem_con' => '<font color="blue">【其他配置】</font> 《《 <font color="blue">[呼叫来电语音相机等死机或崩溃]</font>',
            'otherproblem_con' => '<font color="blue">【其他配置】</font> 《《 <font color="blue">[其他问题]</font>',
            'UserReg' => '<font color="blue">【前台页面】</font> 《《 <font color="blue">[用户注册]</font>',
            'UserZwReg' => '<font color="blue">【前台支付完成】</font> 《《 <font color="blue">[指纹系统开户]</font>',
        );

        $actions = array(
            'add' => '<font color="green"> (新增) </font>',
            'up' => '<font color="orange"> (修改) </font>',
            'del' => '<font color="red"> (删除) </font>',
            'back' => '<font color="green"> (还原) </font>',
            'login' => '<font color="red"> (登录) </font>',
            'logout' => '<font color="red"> (登出) </font>',
            'setNode' => '<font color="#2f4f4f"> (设置权限) </font>',
            'set'   => '<font color="orange"> (设置) </font>',
            'cancel_auth'   => '<font color="orange"> (审核-取消认证) </font>',
            'confirm_auth'   => '<font color="orange"> (审核-认证) </font>',
            'reject_auth'   => '<font color="orange"> (审核-拒绝) </font>',
            'able'   => '<font color="orange"> (解禁) </font>',
            'disable'   => '<font color="orange"> (封禁) </font>',
            'pay'   => '<font color="orange"> (提现) </font>',
            'reject_withdraw'   => '<font color="orange"> (提现-拒绝提现) </font>',
            'edit_price'   => '<font color="orange"> (调价) </font>',
            'edit_emoney'   => '<font color="orange"> (调整E币) </font>',
            'agree'   => '<font color="orange"> (调价审核通过) </font>',
            'set_show_index'   => '<font color="orange"> (设置为首页推荐) </font>',
            'unset_show_index'   => '<font color="orange"> (取消设置为首页推荐) </font>',
            'istop' => '<font color="black"> （置顶） </font>',
            'unistop' => '<font color="#ccc"> （取消置顶） </font>',
            'fail' => '<font color="#333"> （失败） </font>',
        );

        $str = '';
        $str .=!empty($arr[$modules]) ? $arr[$modules] : '';
        $str .=!empty($actions[$action]) ? $actions[$action] . ' 数据' : '';
        if (is_array($id))
            $id = implode(',', $id);
        $str .=!empty($id) ? ' ID为 ' . $id : '';
        $str .= $status == 'success' ? '<font color="green"> 成功 </font>' : '<font color="red"> 失败 </font>';
        $user = !empty(session('Z_AdminiUser')) ? json_decode(session('Z_AdminiUser'),true) : '';
        $data['admin_id'] = !empty($user['id']) ? $user['id'] : '';
        $username = !empty($user['username']) ? $user['username'] : '';
        $data['con'] = '<font color="#8b0000"><b>' .$username. '</b></font>于' . date('Y-m-d H:i:s', time()) . '操作' . $str;
        $data['param'] = json_encode($pram);
        $data['ip'] = self::getIP();
        $data['city'] = $isgetcity ? self::getCity($data['ip']) : '';
        $data['create_time'] = time();
        DB::table('admini_user_log')->insert($data);
    }

    /**
     * 获取真实IP...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @return string
     */
    public static function getIP()
    {
        static $realIp;
        if (isset($_SERVER)) {
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $realIp = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realIp = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realIp = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $realIp = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realIp = getenv("HTTP_CLIENT_IP");
            } else {
                $realIp = getenv("REMOTE_ADDR");
            }
        }

        return $realIp;
    }

    /**
     * ip获取地区信息接口...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $ip
     * @return string
     */
    public static function getCity($ip)
    {
        $ch = curl_init();
        $url = 'http://apis.baidu.com/showapi_open_bus/ip/ip?ip='.$ip;
        $header = array(
            'apikey: 638d3b54efce271713c984ea1cfa57c3',
        );
        // 添加apikey到header
        curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        $city = json_decode($res,true);
        return !empty($city) && key_exists('showapi_res_code',$city) && $city['showapi_res_code'] == 0 ? $city['showapi_res_body']['country'].' '.$city['showapi_res_body']['region'].' '.$city['showapi_res_body']['city'] : '';
    }

    public static function sess($name)
    {
        return session($name);
    }

    /**
     * 获取个人形象自评
     * User: Xiahuiqin
     *
     * @param $id
     *
     * @return string
     */
    public static function getApperance($id){
        if(empty($id)) return '';
        if(is_string($id)){
            $idArr = explode(',',$id);
            $apperance = DB::table('member_auth_sign')->where('status',1)->whereIn('id',$idArr)->get();
        }
        if(!empty($apperance)){
            return $apperance;
        }else{
            return [];
        }
    }

    /**
     * 获取评价标签
     * User: Xiahuiqin
     *
     * @param $id
     *
     * @return string
     */
    public static function getReplyTag($id){
        if(empty($id)) return '';
        if(is_string($id)){
            $idArr = explode(',',$id);
            $tag = DB::table('replay_tag')->select('tag_name','value')->where('status',1)->whereIn('id',$idArr)->get();
        }
        if(!empty($tag)){
            $tag = self::object_to_array($tag);
            return $tag;
        }else{
            return '';
        }
    }

    /**
     * 获取认证图片
     * User: Xiahuiqin
     *
     * @param $id
     *
     * @return string
     */
    public static function getAuthImg($id){
        if(empty($id)) return '';

        $img = DB::table('member_auth_img')->where('status',1)->where('member_id',$id)->get();

        if(!empty($img)){
            return $img;
        }else{
            return '';
        }
    }

    /**
     * 获取真实金额（库中单位为分，这个转化为元）
     * User: Xiahuiqin
     *
     * @param $money
     *
     * @return float|int
     */
    public static function getRealMoney($money){
        if(empty($money)) return 0;
        $money = $money/100;
        return $money;
    }

    /**
     * E币明细状态值
     * User: Xiahuiqin
     *
     * @param      $type
     * @param bool $all
     *
     * @return array|mixed|string
     */
    public static function getEmoneyStatus($type,$all=false){
        $status = array(
            '1' => '充值',
            '2' => '平台调整',
            '3' => '一对一视频消费',
            '4' => '打赏',
            '5' => '提现',
            '6' => '下级消费奖励',
            '7' => '周周榜奖励',
        );
        if($all==true){
            return $status;
        }else{
            if(!empty($status[$type])){
                return $status[$type];
            }else{
                return '无效状态！';
            }
        }
    }

    /**
     * 贡献值明细状态值
     * User: Xiahuiqin
     *
     * @param      $type
     * @param bool $all
     *
     * @return array|mixed|string
     */
    public static function getContributionStatus($type,$all=false){
        $status = array(
            '5' => '提现',
            '6' => '下级充值奖励',
        );
        if($all==true){
            return $status;
        }else{
            if(!empty($status[$type])){
                return $status[$type];
            }else{
                return '无效状态！';
            }
        }
    }

    /**
     * 验证手机号是否合法
     * User: Xiahuiqin
     *
     * @param $phone
     *
     * @return bool
     */
    public static function isMobile($phone) {
        $search = '/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/';
        if ( preg_match( $search, $phone ) ) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取用户收到礼物的金额
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return bool
     */
    public static function getUserGiftMoney($member_id) {
        $money =  DB::table('member_consume')->where('member_id_b',$member_id)->where('type',2)->sum('e_money');
        if(empty($money)){
            return 0;
        }else{
            return $money;
        }
    }

    /**
     * 获取用户等级
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return string
     */
    public static function getUserLevel($member_id)
    {
        $level = '1';
        $used_money = self::getUserUsedMoney($member_id);
        if(!empty($used_money)){
            $level_info = DB::table('level')->where('money','<=',$used_money)->orderBy('money','desc')->first();
            if(!empty($level_info)){
                $level = $level_info->level_name;
            }
        }
        return $level;
    }

    /**
     * 获取用户充值余额
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return bool
     */
    public static function getUserRechargeMoney($member_id) {
        $money =  DB::table('member_recharge')->where('member_id',$member_id)->where('operate_status',1)->where('type',1)->sum('e_money');
        if(empty($money)){
            return 0;
        }else{
            return $money;
        }
    }

    /**
     * 获取用户余额
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return bool
     */
    public static function getUserLeftMoney($member_id) {
        $left_money =  DB::table('member_recharge')->where('member_id',$member_id)->where('operate_status',1)->sum('e_money');
        if(empty($left_money) || $left_money<0){
            return 0;
        }else{
            return $left_money;
        }
    }

    /**
     * 获取用户消费的E币
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return bool
     */
    public static function getUserUsedMoney($member_id) {
        $left_money =  DB::table('member_recharge')->where('member_id',$member_id)->where('operate_status',1)->where('e_money','<',0)->sum('e_money');
        if(empty($left_money)){
            return 0;
        }else{
            //把负数变为正数对外输出
            $left_money = substr($left_money,1);
            return $left_money;
        }
    }

    /**
     * 获取用户下级充值奖励贡献值
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return bool
     */
    public static function getUserRechargeContribution($member_id) {
        $left_money =  DB::table('member_contribution')->where('member_id',$member_id)->where('type',6)->where('operate_status',1)->sum('money');
        if(empty($left_money)){
            return 0;
        }else{
            return $left_money;
        }
    }

    /**
     * 获取用户可提现贡献值
     * User: Xiahuiqin
     *
     * @param $member_id
     *
     * @return bool
     */
    public static function getUserContribution($member_id) {
        $left_money =  DB::table('member_contribution')->where('member_id',$member_id)->where('operate_status',1)->sum('money');
        if(empty($left_money)){
            return 0;
        }else{
            return $left_money;
        }
    }

    /**
     * 获取用户粉丝数
     * User: Xiahuiqin
     *
     * @param $phone
     *
     * @return bool
     */
    public static function getUserFans($member_id) {
        $fans = DB::table('member_fans')->where('member_id_b',$member_id)->where('type',1)->count();
        if(empty($fans)){
            return 0;
        }else{
            return $fans;
        }
    }

    /**
     * 获取用户关注数
     * User: Xiahuiqin
     *
     * @param $phone
     *
     * @return bool
     */
    public static function getUserFocus($member_id) {
        $fans = DB::table('member_fans')->where('member_id_z',$member_id)->where('type',1)->count();
        if(empty($fans)){
            return 0;
        }else{
            return $fans;
        }
    }

    public function sendJpush($content)
    {

        $app_key = 'adaca18863aeee8fb526fbcb';
        $master_secret = 'c6a8c891285ae2933941cb97';
        // 初始化
        $client =new JPush($app_key, $master_secret);
        // 简单推送示例
        $result = $client->push()
            ->setPlatform('all')
            ->addAllAudience()
            ->setMessage($content)
            ->send();
        return $result;
    }
}

/**
 * 排序...
 * @author  <ZhengYaFeng>
 * @access  <public>
 * @date    <Y-m-d H:i:s>
 * @param   <null>
 * @return  <mixed>
 * @des     <描述:...>
 * @param $a
 * @param $b
 * @return int
 */
function menu_sort($a, $b)
{
    if ($a['sort'] == $b['sort']){
        return ($a['id'] < $b['id']) ? -1 : 1;
    }
    return ($a['sort'] > $b['sort']) ? -1 : 1;
}

